<?php
namespace app\index\controller;
// namespace app\zw_admin\controller;
use \think\Controller;
use think\Db;
// use think\Controller;
use think\Session;
use think\Request;


class ClassController extends Controller
{
    public function index()
    {

    $class_goods = Db::name('class_goods')->where(['pid'=>0])->select();
    $class_list = Db::name('class_goods')->select();
    // dump($class_goods);
    // exit;
		$brand_list = Db::name('brand')->select();
		// dump($brand_list);
		// exit;
    $class_name = '';
    $this->assign('list',$brand_list);
    $this->assign('class_name',$class_name);
    $this->assign('class_goods',$class_goods);
		$this->assign('class_list',$class_list);
		return $this->fetch('index');
    }



    public function brand_list()
    {
    	 // dump(input('param.'));
      //     exit;
      	$brand_id = input('param.brand_id');
      	$brand_list = Db::name('selling_brand')->where(['brand_id'=>$brand_id])->select();
      	// dump($brand_list);
      	// exit;
      	$selling_id = null;
      	foreach ($brand_list as $key => $value) {
      		// echo $value['selling_id'];
      		$selling_id .= "`selling_id`="."'".$value['selling_id']."'"." or ";
      	}
      	// exit;
      	$selling_id = rtrim($selling_id,'or ');
      	// echo $selling_id;
      	// exit;
  //     	$selling_id = $brand_list['selling_id'];

      	$selling_list = Db::name('selling')->where($selling_id)->select();
      	return $selling_list;
		// dump($selling_list);
    }




    public function content()
    {
      // dump(input('param.'));
      // exit;
      $brand_id = input('param.brand_id');
      $selling_brand = Db::name('selling_brand')->where(['brand_id'=>$brand_id])->select();
      if (empty($selling_brand)) {
          $selling_list = null;
      }else{
          $selling_id = null;
          foreach ($selling_brand as $key => $v) {
            $selling_id .= "`selling_id`="."'".$v['selling_id']."'"." or ";
          }
          $selling_id = rtrim($selling_id,'or ');
          $selling_list = Db::name('selling')->where($selling_id)->select();
      }
      
      // dump($selling_list);
      // exit;
      $this->assign('selling_list',$selling_list);

      return $this->fetch();
    }



    public function class_index()
    {
      // dump(input('param.'));
      // exit;

      $class_name = input('param.type');
      $list = Db::name('class_goods')
        ->alias('a')
        ->join('drm_brand b','b.class_id = a.id')
        ->where('a.class_name','=',$class_name)
        ->select();
      
      $this->assign('list',$list);
      $this->assign('class_name',$class_name);
      return $this->fetch('class_index');
      // dump($list);
      // exit;

    }






    public function brandlist()
    {
      // dump(input('param.'));
      // exit;
      $selling_id = input('param.selling_id');
      $selling_brand = Db::name('selling_brand')->where(['selling_id'=>$selling_id])->select();
      if (empty($selling_brand)) {
         echo '3';
      }else{
          $brand_id = null;
          foreach ($selling_brand as $key => $v) {
            $brand_id .= "`brand_id`="."'".$v['brand_id']."'"." or ";
          }
          $brand_id = rtrim($brand_id,'or ');
          // echo $brand_id;
          $brand_list = Db::name('brand')->where($brand_id)->select();
          return $brand_list;
          // dump($brand_list);
          // echo json_encode($brand_list);
          // exit;
      }
    }


    /**
     * 预约到店
     * @return [type] [description]
     */
    public function selling_add()
    {
        $uid = Session::get('member_id');
        if (empty($uid)) {
          echo '4';
          exit;
        }
        // dump(input('post.'));
        $list = input('post.');
        $list['uid'] = $uid;


        $about = db('about')->where(['selling_id'=>$list['selling_id'],'uid'=>$list['uid'],'time'=>$list['time']])->select();

        $time = date('Y-m-d');
        if (!empty($about)) {
            if ($about[0]['time'] == $time) {

              echo '3';
              exit;
            }else{

              if (db('about')->insert($list) > 0) {
                echo '1';
              }else{
                echo '2';
              }
            }
        }else{
            if (db('about')->insert($list) > 0) {
                echo '1';
              }else{
                echo '2';
              }
        }
        
        

    } 
}
